using System;
using System.Configuration;
using System.ComponentModel;

namespace TEAM.TimeTracking.View
{
    public class XmlConfig : IConfig, INotifyPropertyChanged
    {

        protected Configuration config;

        public XmlConfig()
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        public bool ConfirmDefaultArrivingTask
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["ConfirmDefaultArrivingTask"]);
            }
            set
            {
                config.AppSettings.Settings["ConfirmDefaultArrivingTask"].Value = value.ToString();
                OnPropertyChanged("ConfirmDefaultArrivingTask");
            }
        }

        public bool ConfirmDefaultLeavingTask
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["ConfirmDefaultLeavingTask"]);
            }
            set
            {
                config.AppSettings.Settings["ConfirmDefaultLeavingTask"].Value = value.ToString();
                OnPropertyChanged("ConfirmDefaultLeavingTask");
            }
        }

        public bool UseRemoteConnection
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["UseRemoteConnection"]);
            }
            set
            {
                config.AppSettings.Settings["UseRemoteConnection"].Value = value.ToString();
                OnPropertyChanged("UseRemoteConnection");
            }
        }

        public string LocalDatabasePath
        {
            get
            {
                return ConfigurationManager.AppSettings["LocalDatabasePath"];
            }
            set
            {
                config.AppSettings.Settings["LocalDatabasePath"].Value = value.ToString();
                OnPropertyChanged("LocalDatabasePath");
            }
        }

        public string RemoteDBHostName
        {
            get
            {
                return ConfigurationManager.AppSettings["RemoteDBHostName"];
            }
            set
            {
                config.AppSettings.Settings["RemoteDBHostName"].Value = value.ToString();
                OnPropertyChanged("RemoteDBHostName");
            }
        }

        public int RemoteDBPort
        {
            get
            {
                return Convert.ToInt32(ConfigurationManager.AppSettings["RemoteDBPort"]);
            }
            set
            {
                config.AppSettings.Settings["RemoteDBPort"].Value = value.ToString();
                OnPropertyChanged("RemoteDBPort");
            }
        }

        public string RemoteDBUser
        {
            get
            {
                return ConfigurationManager.AppSettings["RemoteDBUser"];
            }
            set
            {
                config.AppSettings.Settings["RemoteDBUser"].Value = value.ToString();
                OnPropertyChanged("RemoteDBUser");
            }
        }

        public string RemoteDBPassword
        {
            get
            {
                return ConfigurationManager.AppSettings["RemoteDBPassword"];
            }
            set
            {
                config.AppSettings.Settings["RemoteDBPassword"].Value = value.ToString();
                OnPropertyChanged("RemoteDBPassword");
            }
        }

        public string UserName
        {
            get
            {
                return ConfigurationManager.AppSettings["UserName"];
            }
            set
            {
                config.AppSettings.Settings["UserName"].Value = value.ToString();
                OnPropertyChanged("UserName");
            }
        }

        public string UserPassword
        {
            get
            {
                return ConfigurationManager.AppSettings["UserPassword"];
            }
            set
            {
                config.AppSettings.Settings["UserPassword"].Value = value.ToString();
                OnPropertyChanged("UserPassword");
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public string ExcelExportTemplatesFolder
        {
            get
            {
                string result = ConfigurationManager.AppSettings["ExcelExportTemplatesFolder"];
                return result;
            }
            set
            {
                config.AppSettings.Settings["ExcelExportTemplatesFolder"].Value = value.ToString();
                OnPropertyChanged("ExcelExportTemplatesFolder");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
